package com.example.sesata.service.impl;

import com.example.sesata.dao.OrderDao;
import com.example.sesata.domain.Order;
import com.example.sesata.service.AccountService;
import com.example.sesata.service.OrderService;
import com.example.sesata.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author chensixiang chensixiang1234@gmail.com
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class OrderServiceImpl implements OrderService {

    private final OrderDao orderDao;
    private final AccountService accountService;
    private final StorageService storageService;

    @Override
    @GlobalTransactional(name = "fsp-create-order", rollbackFor = Exception.class)
    public void create(Order order) {
        log.info("---------> 开始新建订单");
        orderDao.insert(order);
        log.info("---------> 订单微服务开始调用库存，做扣减Count");
        System.out.println(order);
        storageService.decrease(order.getProductId(), order.getCount());
        log.info("---------> 订单微服务开始调用库存，做扣减Count end");
        log.info("---------> 账户微服务开始调用账户，做扣减Money");
        accountService.decrease(order.getUserId(), order.getMoney());
        log.info("---------> 账户微服务开始调用账户，做扣减Money end");
        log.info("---------> 修改订单状态");
        orderDao.update(order.getUserId(), 0);
        log.info("---------> 修改订单结束");
        log.info("---------> 下订单结束结束了，O(∩_∩)O哈哈~");
    }
}
